package cn.tedu.xuecha.server.adapter.controller;


import cn.tedu.xuecha.server.adapter.assemble.RequestAssemble;
import cn.tedu.xuecha.server.adapter.protocol.vo.ContentVO;
import cn.tedu.xuecha.server.protocol.query.ContentQuery;
import cn.tedu.xuecha.server.service.ContentService;
import cn.tedu.xuecha.server.protocol.param.ContentParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Api(tags = "01. 内容表管理模块")
@Validated
@RestController
@RequestMapping("/contents")
public class ContentController {

    @Autowired
    ContentService contentService;
    public ContentController() {
        log.info("创建控制器对象：AdminController");
    }

    @Autowired
    RequestAssemble requestAssemble;

    @ApiOperation("添加内容")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public Long addNew(ContentParam contentParam) {

        log.debug("开始处理【添加内容表】的请求，参数：{}", contentParam);
        return  contentService.insert(requestAssemble.assembleParame2BO(contentParam));

    }


    @ApiOperation("根据id删除内容")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "内容表id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/delete")
    public int delete(@Range(min = 1, message = "删除内容表失败，尝试删除的内容表的ID无效！")
                                   @PathVariable Integer id) {
        log.debug("开始处理【根据id删除内容表】的请求，参数：{}", id);

        return contentService.delete(id);
    }

    @ApiOperation("根据id修改内容")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "内容表id", required = true, dataType = "long")
    @PostMapping("/update")
    public int update(ContentParam contentParam) {
        log.debug("开始处理【根据id修改内容表】的请求，参数：{}", contentParam);

        return contentService.update(requestAssemble.assembleParame2BO(contentParam));
    }
//    // http://localhost:9081/contents
    @ApiOperation("查询内容列表")
    @ApiOperationSupport(order = 420)
    @GetMapping("")
    public List<ContentVO> list() {
        log.debug("开始处理【查询内容表列表】的请求，无参数");
        List<ContentQuery> list1 = contentService.list();
        List<ContentVO> list = new ArrayList<>();
        for (ContentQuery contentQuery:list1) {
            ContentVO contentVO = requestAssemble.assembleDTO2VO(contentQuery);
            list.add(contentVO);
        }
        return list;
    }


}